package cn.smbms.contorller.bill;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.StringUtils;

import cn.smbms.contorller.user.LoginController;
import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.bill.BillService;
import cn.smbms.service.bill.BillServiceImpl;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.service.provider.ProviderServiceImpl;
import cn.smbms.tools.ACT;
import cn.smbms.tools.Constants;
import cn.smbms.tools.EncodingTool;


@Controller
@RequestMapping("/Bill")
public class BillController {
	private static Logger logger=Logger.getLogger(LoginController.class);

	
	/*
	 * 订单业务实现类属性对象
	 */
	@Resource(name="billServiceImpl")
	private BillService billService;
	
	/*
	 * 供应商业务实现类属性对象
	 */
	@Resource(name="providerServiceImpl")
	private ProviderService providerService;
	
	/*订单方法功能判断*/
	@RequestMapping(value="/entrance",method=RequestMethod.GET)
	public String entrance(
			@RequestParam(value="method",required=true)String method,
			HttpServletRequest request, HttpServletResponse response){

		if(method != null && method.equals("query")){
			/*this.query(request, response);*/
			logger.debug("=====================query");
			return "redirect:/Bill/getBill";
		}
		return "";
	}


	/*订单信息查看*/
	@RequestMapping(value="/getBill",method=RequestMethod.GET)
	public String query(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value="queryProductName",required=false)String queryProductName1,
			@RequestParam(value="queryProviderId",required=false)String queryProviderId1,
			@RequestParam(value="queryIsPayment",required=false)String queryIsPayment1)
					throws ServletException, IOException {

		List<Provider> providerList = new ArrayList<Provider>();
		providerList = providerService.getProviderList("","");
		request.setAttribute("providerList", providerList);

		String queryProductName = queryProductName1;
		String queryProviderId = queryProviderId1;
		String queryIsPayment = queryIsPayment1;
		if(StringUtils.isNullOrEmpty(queryProductName)){
			queryProductName = "";
		}

		List<Bill> billList = new ArrayList<Bill>();
		Bill bill = new Bill();
		if(StringUtils.isNullOrEmpty(queryIsPayment)){
			bill.setIsPayment(0);
		}else{
			bill.setIsPayment(Integer.parseInt(queryIsPayment));
		}

		if(StringUtils.isNullOrEmpty(queryProviderId)){
			bill.setProviderId(0);
		}else{
			bill.setProviderId(Integer.parseInt(queryProviderId));
		}
		bill.setProductName(queryProductName);
		billList = billService.getBillList(bill);
		request.setAttribute("billList", billList);
		request.setAttribute("queryProductName", queryProductName);
		request.setAttribute("queryProviderId", queryProviderId);
		request.setAttribute("queryIsPayment", queryIsPayment);
		return "billlist";

	}


	/*
	 * 订单查看信息
	 */
	@RequestMapping(value="/view")
	public String getBillById(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value="billid",required=true)String billid1)
					throws ServletException, IOException {
		String id = billid1;
		if(!StringUtils.isNullOrEmpty(id)){
			Bill bill = null;
			bill = billService.getBillById(id);
			request.setAttribute("bill", bill);

		}
		return "billview";
	}

	/*
	 * 查看订单修改信息页面
	 */
	@RequestMapping(value="/billInfo",method=RequestMethod.GET)
	public String billModiy(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("billid")String billid1)
					throws ServletException, IOException {
		String id = billid1;
		if(!StringUtils.isNullOrEmpty(id)){
			Bill bill = null;
			bill = billService.getBillById(id);
			request.setAttribute("bill", bill);
		}
		return "billmodify";
	}
	
	
	/*
	 * 获取供应商名称列表
	 */
	@RequestMapping(value="/getProviderlist",method=RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getProviderlist(HttpServletRequest request,
			HttpServletResponse response)
			throws ServletException, IOException {
		
		System.out.println("getproviderlist ========================= ");
		
		List<Provider> providerList = new ArrayList<Provider>();
		providerList = providerService.getProviderList("","");
		
		
		String json=JSONArray.toJSONString(providerList);
		return json;
		
	}
	
	
	/*
	 * 提交保存订单信息
	 */
	@RequestMapping(value="/submitModify",method=RequestMethod.POST)
	public String modify(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id")String id1,
			@RequestParam("productName")String productName1,
			@RequestParam("productUnit")String productUnit1,
			@RequestParam("productCount")String productCount1,
			@RequestParam("totalPrice")String totalPrice1,
			@RequestParam("providerId")String providerId1,
			@RequestParam("isPayment")String isPayment1
			)
			throws ServletException, IOException {
		System.out.println("modify===============");
		String id =EncodingTool.encodeStr(id1);
		String productName = productName1;
		
		String productUnit = productUnit1;
		String productCount = productCount1;
		String totalPrice = totalPrice1;
		String providerId = providerId1;
		String isPayment = isPayment1;
		
		Bill bill = new Bill();
		bill.setId(Integer.valueOf(id));
		bill.setProductName(productName);
		
		bill.setProductUnit(productUnit);
		bill.setProductCount(new BigDecimal(productCount).setScale(2,BigDecimal.ROUND_DOWN));
		bill.setIsPayment(Integer.parseInt(isPayment));
		bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2,BigDecimal.ROUND_DOWN));
		bill.setProviderId(Integer.parseInt(providerId));
		
		bill.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
		bill.setModifyDate(new Date());
		Date da=bill.getModifyDate();
		Date de=new java.sql.Date(da.getTime());
		bill.setModifyDate(de);
		boolean flag = false;
		flag = billService.modify(bill);
		if(flag){
			return "redirect:/Bill/getBill";
		}else{
			return "billmodify";
		}
	}
	
	
	/*
	 * s删除订单
	 */
	@RequestMapping(value="/delBill",method=RequestMethod.GET)
	@ResponseBody
	public String delBill(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("billid")String billid1
			)
			throws ServletException, IOException {
		String id = billid1;
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(!StringUtils.isNullOrEmpty(id)){
			boolean flag = billService.deleteBillById(id);
			if(flag){//删除成功
				resultMap.put("delResult", "true");
			}else{//删除失败
				resultMap.put("delResult", "false");
			}
		}else{
			resultMap.put("delResult", "notexit");
		}
		String json=JSONObject.toJSONString(resultMap);
		System.out.println(json);
			return json;
	}
	
	/*
	 * 订单添加提交
	 */
	@RequestMapping(value="/addBill",method=RequestMethod.POST)
	public String add(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("billCode")String billCode1,
			@RequestParam("productName")String productName1,
			@RequestParam("productUnit")String productUnit1,
			@RequestParam("productCount")String productCount1,
			@RequestParam("totalPrice")String totalPrice1,
			@RequestParam("providerId")String providerId1,
			@RequestParam("isPayment")String isPayment1
			)
			throws ServletException, IOException {
		String billCode = billCode1;
		String productName = productName1;
		String productUnit = productUnit1;
		
		String productCount =productCount1;
		String totalPrice = totalPrice1;
		String providerId = providerId1;
		String isPayment =isPayment1;
		
		Bill bill = new Bill();
		bill.setBillCode(billCode);
		bill.setProductName(productName);
		bill.setProductUnit(productUnit);
		bill.setProductCount(new BigDecimal(productCount).setScale(2,BigDecimal.ROUND_DOWN));
		bill.setIsPayment(Integer.parseInt(isPayment));
		bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2,BigDecimal.ROUND_DOWN));
		bill.setProviderId(Integer.parseInt(providerId));
		bill.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
		bill.setCreationDate(new Date());
		Date da=bill.getCreationDate();
		Date de=new java.sql.Date(da.getTime());
		bill.setCreationDate(de);
		boolean flag = false;
		flag = billService.add(bill);
		System.out.println("add flag -- > " + flag);
		if(flag){
			return "redirect:/Bill/getBill";
		}else{
			return "billmodify";
		}
		
	}
}

